<?php if(!defined('CALL')) exit('No direct script access allowed');

include_once('lib/xtemplate.class.php');

define("PAGE_TEMPLATE", "xtpl/page.xtpl");

abstract class Page
{
	private $js_file = "page-empty.js";
	private $css_file = "page-empty.css";
	protected $viewer = null;

	abstract protected function add_css();
	abstract protected function add_js();
	abstract protected function fill_header($xtpl);
	abstract protected function fill_footer($xtpl);
	abstract protected function fill_body($xtpl);

	public function __construct($viewer, $js_file = null, $css_file = null)
	{
		$this->viewer = $viewer;
		
		if($js_file) $this->js_file = $js_file;
		if($css_file) $this->css_file = $css_file;
	}

	protected function build_framework()
	{
		$xtpl = new XTemplate(PAGE_TEMPLATE);
		$xtpl->assign('root', ROOT_URL);
		$xtpl->assign('title', TITLE);

		$xtpl->assign('css_file', $this->css_file);
		$xtpl->assign('js_file', $this->js_file);
		
		$xtpl->assign('page_css', $this->add_css());
		$xtpl->assign('page_js', $this->add_js());

		foreach ($_GET as $key => $val)
		$xtpl->assign($key, $val);
			
		return $xtpl;
	}

	public function out()
	{
		header('Content-Type: text/html; charset=utf-8');
		// header('Content-Type: text/plain; charset=utf-8');
		$xtpl = $this->build_framework();

		$this->fill_header($xtpl);
		$this->fill_body($xtpl);
		$this->fill_footer($xtpl);

		$xtpl->parse('root');

		echo $xtpl->out('root');
		echo "";
	}
}
